# 案例：打开‘注册实例.html’页面，完成以下操作
# 1). 填写主页面的注册信息
# 2). 填写注册页面A中的注册信息
# 3). 填写注册页面B中的注册信息
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 1. 打开浏览器
driver = webdriver.Chrome(executable_path='../01/chromedriver.exe')
driver.maximize_window()

# 2. 访问本地页面
url = r"E:\pythonProject\UI自动化测试\注册实例.html"
driver.get(url)

# 1). 填写主页面的注册信息
driver.find_element(By.CSS_SELECTOR, "#user").send_keys("admin")
driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
driver.find_element(By.CSS_SELECTOR, "#tel").send_keys("13012345678")
driver.find_element(By.CSS_SELECTOR, "#email").send_keys("123@qq.com")

# 点击“注册A页面”链接（会打开新窗口）
driver.find_element_by_partial_link_text("A网页").click()

# ========== 关键步骤：切换到新窗口的句柄 ==========
windows = driver.window_handles  # 获取所有窗口句柄
driver.switch_to.window(windows[-1])  # 切换到最后一个新窗口

# 2). 填写注册页面A中的注册信息
# 等待元素加载（可选，确保元素存在）
WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, "#userA"))
)
driver.find_element(By.CSS_SELECTOR, "#userA").send_keys("1111")
driver.find_element(By.CSS_SELECTOR, "#passwordA").send_keys("123456")
driver.find_element(By.CSS_SELECTOR, "#telA").send_keys("13012345678")
driver.find_element(By.CSS_SELECTOR, "#emailA").send_keys("123@qq.com")

sleep(3)
driver.quit()